package com.ruoyi.system.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ruoyi.common.annotation.DataColumn;
import com.ruoyi.common.annotation.DataPermission;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
import com.ruoyi.system.domain.Dto.ClockUserDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 部门管理 数据层
 *
 * @author Lion Li
 */
public interface SysDeptMapper extends BaseMapperPlus<SysDeptMapper, SysDept, SysDept> {

    /**
     * 查询部门管理数据
     *
     * @param queryWrapper 查询条件
     * @return 部门信息集合
     */
    @DataPermission({
        @DataColumn(key = "deptName", value = "dept_id")
    })
    List<SysDept> selectDeptList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);

    /**
     * 查询部门管理数据(全部）
     *
     * @param queryWrapper 查询条件
     * @return 部门信息集合
     */
    List<SysDept> selectDeptListAll(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);

    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId            角色ID
     * @param deptCheckStrictly 部门树选择项是否关联显示
     * @return 选中部门列表
     */
    List<Long> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);



    @Select("SELECT dept_name \n" +
        "FROM sys_dept\n" +
        "\n" +
        "LEFT JOIN sys_user ON sys_dept.dept_id=sys_user.dept_id\n" +
        "WHERE sys_user.user_name=#{userName} limit 1")
    SysDept getDetInfoByUserName(@Param("userName") String  userName);


    @Select("SELECT *\n" +
        "FROM sys_dept\n" +
        "WHERE dept_id NOT IN (SELECT parent_id FROM sys_dept)")
    List<SysDept> selectAllSonData();


    //根据id集合获取所有id的
    @Select("\tSELECT u.user_id,nick_name , GROUP_CONCAT(r.role_name SEPARATOR ', ') roleName,dept.dept_name deptName\n" +
        "FROM sys_user u left JOIN sys_user_role uc ON u.user_id = uc.user_id left JOIN sys_role r ON uc.role_id = r.role_id\n" +
        "left JOIN sys_dept dept ON u.dept_id = dept.dept_id\n" +
        "WHERE u.dept_id IN (SELECT dept_id FROM sys_dept WHERE(SELECT COUNT(*) FROM sys_dept AS t2 WHERE\tFIND_IN_SET( t2.dept_id, sys_dept.ancestors ) > 0 \n" +
        "\tAND t2.dept_id IN (#{ids})) > 0 UNION SELECT dept_id FROM sys_dept WHERE dept_id IN (#{ids}) ) and u.status=0 and u.del_flag=0\n" +
        "\tGROUP BY u.user_id")
    List<ClockUserDto> selectSonDateByIds(@Param("ids") String ids);


    @Select("SELECT roles\n" +
        "FROM sys_user u\n" +
        "JOIN sys_user_role uc ON #{userId} = uc.user_id\n" +
        "JOIN sys_role r ON uc.role_id = r.role_id\n" +
        "GROUP BY u.user_id;")
    String selectUserRolesByUserId(@Param("userId") String userId);
}
